﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WPFServices.Converters
{
    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool boolValue)
            {
                return !boolValue;
            }
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool boolValue)
            {
                return !boolValue;
            }
            return false;
        }
    }

    public class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isInverse = parameter as string == "inverse";
            
            if (value is string stringValue)
            {
                bool hasValue = !string.IsNullOrWhiteSpace(stringValue);
                return (hasValue != isInverse) ? Visibility.Visible : Visibility.Collapsed;
            }
            else if (value != null)
            {
                // 对于非字符串对象，检查是否为null
                bool hasValue = true;
                return (hasValue != isInverse) ? Visibility.Visible : Visibility.Collapsed;
            }
            else
            {
                // 值为null
                bool hasValue = false;
                return (hasValue != isInverse) ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class FromTypeToIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int fromType)
            {
                // 1=销售订单, 2=预测订单, 3=其他
                return fromType - 1; // 转换为0-based索引
            }
            return 0; // 默认选择第一个
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int index)
            {
                return index + 1; // 转换回1-based值
            }
            return 1; // 默认销售订单
        }
    }
}